package com.usian.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;


@Configuration
@ConfigurationProperties(prefix="jdbc")
public class DataSourceConfig {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    private String url;
    private String driverClassName;
    private String username;
    private String password;

    public DataSourceConfig setUrl(String url) {
        this.url = url;
        return this;
    }

    public DataSourceConfig setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
        return this;
    }

    public DataSourceConfig setUsername(String username) {
        this.username = username;
        return this;
    }

    public DataSourceConfig setPassword(String password) {
        this.password = password;
        return this;
    }

    @Bean // <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    public DruidDataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
